package cn.xxc.org.service.impl;

import cn.xxc.basic.service.impl.BaseServiceImpl;
import cn.xxc.org.domain.Department;
import cn.xxc.org.mapper.DepartmentMapper;
import cn.xxc.org.service.DepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements DepartmentService {
    @Autowired
    private DepartmentMapper departmentMapper;

    /**
     * 联级
     * @return
     */
    @Override
    @Transactional
    public List<Department> getTree(){
        List<Department> list=new ArrayList<>();
        List<Department> loadAll = departmentMapper.loadAll();
        Map<Long,Department> map=new HashMap<>();
        for (Department department : loadAll) {
            map.put(department.getId(), department);
        }
        for (Department department : loadAll) {
            if (department.getParent_id() == null) {
                list.add(department);
            }else {
                map.get(department.getParent_id()).getChildren().add(department);
            }
        }
        return list;
    }
}
